<template>
  <el-dialog
    :title="formData.id?'编辑角色':'新增角色'"
    :visible="visibleDialog"
    @close="closeFn"
  >
    <el-form
      ref="form"
      label-width="120px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="用户名称" prop="title">
        <el-input v-model="formData.title" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="用户描述" prop="description">
        <el-input v-model="formData.description" placeholder="请输入角色描述" />
      </el-form-item>
      <template>
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="submit">提交</el-button>
          <el-button size="small" @click="closeFn">取消</el-button>
        </el-row>
      </template>
    </el-form>
  </el-dialog>
</template>

<script>
import { addRoles, updateRoleId } from '@/api/roles'
export default {
  props: {
    visibleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        description: ''
      },
      rules: {
        title: [{
          required: true, message: '请输入角色名称', trigger: 'blur'
        }],
        description: [{
          required: true, message: '请输入角色描述', trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    // 关闭弹窗
    closeFn() {
      this.$emit('update:visibleDialog', false)
      this.formData = {
        title: '',
        content: ''
      }
      this.$refs.form.resetFields()
    },
    // 新增
    async submit() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await updateRoleId(this.formData)
      } else {
        await addRoles(this.formData)
      }
      this.closeFn()
      this.$emit('updateFn')
    }
  }
}
</script>

<style>

</style>
